package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjPrintQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.DateUtils;
import com.koron.util.PrintInterfaceImpl;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.*;

/**
 * 施工合同模板打印
 */
public class PjConstructionPrint implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPrintQuery pjPrintQuery = JsonUtils.objectToPojo(req.getData(), PjPrintQuery.class);

            // 1. 获取打印数据
            ArrayList<HashMap<String, String>> result = getPrintDate(factory, pjPrintQuery,userInfo);
            // 2. 生成打印id
            String key = PrintInterfaceImpl.setPrintData(result);
            List<String> keys = new ArrayList<String>();
            keys.add(key);
            // 3. 返回打印id
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "施工合同模板打印成功", List.class);
            messageBean.setData(keys);
            return messageBean;
        } catch (Exception e) {
            logger.error("施工合同模板打印数据获取失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "施工合同模板打印失败", null);
        }
    }

    private ArrayList<HashMap<String, String>> getPrintDate(SessionFactory factory, PjPrintQuery pjPrintQuery, UserInfoBean userInfo) {
        ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
        PjInfoMapper mapper = factory.getMapper(PjInfoMapper.class);
        CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
        MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
        BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);

        PjInfoBean pjInfoBean = mapper.selectById(pjPrintQuery.getConsultId());
        CtmInfoBean ctmInfoBean = ctmMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), CtmInfoBean.CTMTEMP);
        MeterBoreBean meterBore = meterBoreMapper.selectMeterBoreById(pjInfoBean.getMeterBore());
        String areaName = businessAreaMapper.findBusinessAreaById(pjInfoBean.getBusinessArea(), userInfo.getCurWaterCode());;


        Map<String, String> pjTypeMap = Tools.mapDicByCode(factory,"PPT");
        Map<String, String> certTypeMap = Tools.mapDicByCode(factory,"IDT");
        HashMap<String, String> hashMap=new HashMap<>();
        // 工程安装地址
        hashMap.put("installAddr",Objects.toString(pjInfoBean.getInstallAddr(), ""));
        // 客户名称
        hashMap.put("ctmName",Objects.toString(ctmInfoBean.getCtmName(), ""));
        // 证件类型
        hashMap.put("certNo", Objects.toString(ctmInfoBean.getCertNo(), ""));
        // 联系电话
        hashMap.put("linkTel",Objects.toString(ctmInfoBean.getLinkTel(), ""));
        // 手机号
        hashMap.put("mobile", Objects.toString(ctmInfoBean.getMobile(), ""));
        // 单据编号
        hashMap.put("billNo",pjInfoBean.getBillNo());
        // 单据日期
        hashMap.put("billDate", DateUtils.parseDate(pjInfoBean.getBillDate(), "yyyy-MM-dd"));
        // 工程名称
        hashMap.put("pjName",pjInfoBean.getPjName());
        // 工程类型
        hashMap.put("pjType", Objects.toString(pjTypeMap.get(pjInfoBean.getPjType()), ""));
        // 证件类型
        hashMap.put("certTypeName", Objects.toString(certTypeMap.get(ctmInfoBean.getCertType()), ""));
        // 区域名称
        hashMap.put("areaName", Objects.toString(areaName, ""));
        // 水表口径
        hashMap.put("meterBore", Objects.toString(meterBore.getBoreName(), ""));
        result.add(hashMap);
        return result;
    }
}
